/*
============================================================================
FileName	:	XQChatClientManager.h
Author		:	liyake
Website		:	http://www.liyake.com
Create Data	:	2012-03-08 13:07
Change Data	:	2012-03-08 13:07
Detail		:	The socket client of xqchat application
============================================================================
*/

#include "XQChatClientManager.h"
#include "XQChatRenrenClient.h"

using namespace XQChat;

XQChat::CXQChatClientManager::CXQChatClientManager(void)
	: m_pRosterManager(0)
{
	m_pRosterManager = CXQChatRosterManager::getRosterManager();
}

XQChat::CXQChatClientManager::~CXQChatClientManager(void)
{
	delete m_pRosterManager;
}

CXQChatClientManager* XQChat::CXQChatClientManager::getQXChatClientManager()
{
	if(!m_pClientManager)
		m_pClientManager = new CXQChatClientManager();

	return m_pClientManager;
}

CXQChatClient* XQChat::CXQChatClientManager::addAccount( const QString& jid, const QString& password )
{
	if(m_mapClients.find(jid)!=m_mapClients.end())
		return 0;

	CXQChatConfiguration config(jid,password);
	CXQChatClient* pClient = 0;

	switch(config.getClientType())
	{
	case CXQChatConfiguration::XQChatRenren:
		pClient = new CXQChatRenrenClient(config);		//renren, use class CXQChatRenrenClient.
		break;
	case CXQChatConfiguration::XQChatGTalk:
		pClient = new CXQChatClient(config);			//gtalk, use default client
		break;
	case CXQChatConfiguration::XQChatMSN:
		pClient = new CXQChatClient(config);			//msn, use default client
		break;
	default:
		pClient = new CXQChatClient(config);
		break;
	}
	pClient->connectToServer();

	m_mapClients.insert(jid,pClient);

	connect(pClient,SIGNAL(connected()),this,SLOT(onClientConnected()));
	connect(pClient,SIGNAL(disconnected()),this,SLOT(onClientDisconnected()));
	connect(pClient,SIGNAL(error(QXmppClient::Error)),
		this,SLOT(onClientError(QXmppClient::Error)));
	connect(pClient,SIGNAL(messageReceived(const QXmppMessage&)),
		this,SLOT(onClientMsgReceived(const QXmppMessage&)));
	connect(pClient,SIGNAL(presenceReceived(const QXmppPresence&)),
		this,SLOT(onClientPresenceReceived(const QXmppPresence&)));
	connect(pClient,SIGNAL(stateChanged(QXmppClient::State)),
		this,SLOT(onClientStateChanged(QXmppClient::State)));

	connect(pClient,SIGNAL(rosterReceived()),this,SIGNAL(rosterReceived()));
	connect(pClient,SIGNAL(presenceChanged(const QString&, const QString&)),
		this,SIGNAL(presenceChanged(const QString&, const QString&)));

	return pClient;
}

CXQChatRosterManager* XQChat::CXQChatClientManager::getRosterManager()
{
	return m_pRosterManager;
}

void XQChat::CXQChatClientManager::onClientConnected()
{

}

void XQChat::CXQChatClientManager::onClientDisconnected()
{

}

void XQChat::CXQChatClientManager::onClientError( QXmppClient::Error error )
{

}

void XQChat::CXQChatClientManager::onClientMsgReceived( const QXmppMessage &message )
{

}

void XQChat::CXQChatClientManager::onClientPresenceReceived( const QXmppPresence &presence )
{
}

void XQChat::CXQChatClientManager::onClientStateChanged( QXmppClient::State state )
{

}

CXQChatClientManager* XQChat::CXQChatClientManager::m_pClientManager = 0;
